package cn.com.nthotline.db;

import java.util.ArrayList;
import java.util.List;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import cn.com.nthotline.model.SearchHistory;

public class DBSearchHistory implements DBInterface<SearchHistory> {
	private DatabaseHelper helper;

	public DBSearchHistory(Context context) {
		helper = new DatabaseHelper(context);
	}

	@Override
	public long insert(SearchHistory t) {
		// TODO Auto-generated method stub
		ContentValues values = new ContentValues();
		values.put(SearchHistory.CATID, t.catId);
		values.put(SearchHistory.KEYWORD, t.keyWord);
		return helper
				.insertData(CreateDbConstants.TABLE_SEARCH_HISTORY, values);
	}

	@Override
	public List<SearchHistory> query(String id) {
		// TODO Auto-generated method stub
		List<SearchHistory> list = new ArrayList<SearchHistory>();
		SQLiteDatabase db = helper.getWritableDatabase();
		Cursor cursor = db.rawQuery("select catid,keyword from "
				+ CreateDbConstants.TABLE_SEARCH_HISTORY, null);
		while (cursor.moveToNext()) {
			SearchHistory history = new SearchHistory();
			history.catId = cursor.getString(cursor
					.getColumnIndex(SearchHistory.CATID));
			history.keyWord = cursor.getString(cursor
					.getColumnIndex(SearchHistory.KEYWORD));
			list.add(history);
		}
		return list;
	}
	public void deleteTable(){
		helper.deleteTable(CreateDbConstants.TABLE_SEARCH_HISTORY);
	}
}
